import requests
import re

from maltego_trx.entities import Image

from maltego_trx.transform import DiscoverableTransform


class PersonToImages(DiscoverableTransform):
    """
    Receive DNS name from the client, and resolve to IP address.
    """

    @classmethod
    def create_entities(cls, request, response):
        person_name = request.Value
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36",
            "referer": "https://image.baidu.com"
        }
        url = "http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=" + person_name
        req = requests.get(url,headers=headers)
        f = req.text
        key = r'thumbURL":"(.+?)"'
        key1 = re.compile(key)
        for string in re.findall(key1, f):
            response.addEntity(Image,string)

